package com.base.project.filters.filterLists;

import com.alibaba.fastjson.JSONObject;
import com.base.project.filters.utils.BodyReaderHttpServletRequestWrapper;
import com.base.project.filters.utils.HttpUtils;
import com.base.project.filters.utils.SignUtil;
import com.base.project.resultMapUtil.ResultMapUtil;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.SortedMap;

/**
 * @ProjectName: project
 * @Package: com.base.project.filters.filterLists
 * @Description: java类作用描述
 * @Author: Z.G.C
 * @CreateDate: 2020/5/15 4:10 PM
 * @UpdateUser: Z.G.C
 * @UpdateDate: 2020/5/15 4:10 PM
 * @UpdateRemark: The modified content
 * @Version: 1.0
 * <p>Copyright: Copyright (c) 2020</p>
 */
@Slf4j
public class SignAuthFilter implements Filter{


    @Override
    public void init(FilterConfig filterConfig) {
        System.out.println();
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

        HttpServletResponse response = (HttpServletResponse) res;
        // 防止流读取一次后就没有了, 所以需要将流继续写出去
        HttpServletRequest request = (HttpServletRequest) req;
        //解决跨域的问题
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials","true");
        response.setHeader("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With,X-App-Id, X-Token");
        response.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
        response.setHeader("Access-Control-Max-Age", "3600");

        String path=request.getRequestURI();
        log.info(path);



        HttpServletRequest requestWrapper = new BodyReaderHttpServletRequestWrapper(request);
        boolean pathBool = verfilyUrl(path);
        if(pathBool){
            chain.doFilter(requestWrapper, response);
            return ;
        }
        //获取全部参数(包括URL和body上的)
        SortedMap<String, Object> allParams = HttpUtils.getAllParams(requestWrapper);
        //对参数进行签名验证
        boolean isSigned = SignUtil.verifySign(allParams);
        if (isSigned) {
            System.out.println("签名通过");
            System.out.println(isSigned);
            chain.doFilter(requestWrapper, response);
            return ;
        } else {
            System.out.println("参数校验出错");
            //校验失败返回前端
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            PrintWriter out = response.getWriter();
            out.append(ResultMapUtil.error("INVALID_SIGN","无效签名").toString());
            return ;
        }
        // 多次请求 没有时间戳
    }

    @Override
    public void destroy() {

    }
    private boolean verfilyUrl(String url) {
        List<String> list = new ArrayList<String>();
        list.add("/airline/api/payAsync/updateOrderWx");
        list.add("/airline/api/payAsync/updateOrderAli");
        list.add("/airline/api/wechat/navigationMap");
        if (list.contains(url)) {
            return true;
        }
        return false;


    }


}
